package servlet;

import bean.Result;
import com.google.gson.Gson;
import dao.QueryUserdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/saveuser.s")
public class SaveUserServlet  extends HttpServlet {

    QueryUserdao dao=new QueryUserdao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String username = req.getParameter("username");
        final String telphone = req.getParameter("telphone");
        final String email = req.getParameter("email");
        final HttpSession session = req.getSession();
        final Object user = session.getAttribute("user");
        Result result;
        if (username.trim().isEmpty()==false&&telphone.trim().isEmpty()==false&&email.trim().isEmpty()==false){
            dao.save(username,telphone,email, (String) user);
            session.setAttribute("user",username);
            result=new Result(1,"保存成功",null);
        }else {
            result =new Result(0,"输入错误",null);
        }
        final String json = new Gson().toJson(result);
        resp.getWriter().append(json);
    }
}
